package com.food.configure;


import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间格式化工具
 */

@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public ObjectMapper jacksonOnjectMapper(Jackson2ObjectMapperBuilder builder){
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        //json转换日期格式设置
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule simpleModule = new SimpleModule();
        //在进出前后台的时候，设置BigDecimal和字符串之间转换
        simpleModule.addSerializer(BigDecimal.class,ToStringSerializer.instance);
        //设置对前台传入的多种格式的时间都支持序列化
        simpleModule.addDeserializer(Date.class, new StdScalarDeserializer<Date>(Date.class) {
            @Override
            public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
                JsonToken t = jsonParser.getCurrentToken();
                if (t == JsonToken.VALUE_STRING) {
                    String str = jsonParser.getText().trim();
                    if (str.length() == 0) {
                        return null;
                    }
                    try {
                        return DateUtils.parseDate(str, "yyyy-MM-dd","yyyy/MM/dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","yyyy-MM-dd'T'HH:mm:ss.SSS+00:00");
                    } catch (ParseException e) {
                        return (Date)deserializationContext.handleWeirdStringValue(handledType(),str,"expected format \"%s\"");
                    }
                }

                if (t == JsonToken.START_ARRAY && deserializationContext.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
                    jsonParser.nextToken();
                    final Date parsed = _parseDate(jsonParser,deserializationContext);
                    t = jsonParser.nextToken();
                    if (t != JsonToken.END_ARRAY) {
                        handleMissingEndArrayForSingle(jsonParser,deserializationContext);
                    }
                    return parsed;
                }
                return super._parseDate(jsonParser,deserializationContext);
            }
        });
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
